//
//  DSPersonalViewController.m
//  外星球生活圈
//
//  Created by MR.ZHOU on 15/8/14.
//  Copyright (c) 2015年 MR.ZHOU. All rights reserved.
//

#import "ZFPersonalViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIButton+WebCache.h"
#import "NSData+Base64.h"
#import "ZFLoginViewController.h"
#import "ZFQuteView.h"
@interface ZFPersonalViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *imageBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property(nonatomic,strong)ZFQuteView *quteView;

@end

@implementation ZFPersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"个人中心";
    
    [ _imageBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:[ZFUserInfo shareInstance].middle_avatar] forState:UIControlStateNormal];
    _imageBtn.layer.cornerRadius = CGRectGetHeight(_imageBtn.frame)/2.0;
    _imageBtn.layer.masksToBounds = YES;
    
}

-(void)backMethod{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app.tab pushHidden:NO];
    
}
- (IBAction)imageBtnClick:(UIButton *)sender {
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相册" otherButtonTitles:@"拍照 ", nil];
    [sheet showInView:self.view];
}
- (IBAction)cancleBtnClick:(UIButton *)sender {
    _quteView =[[[NSBundle mainBundle]loadNibNamed:@"ZFQuteView" owner:nil options:nil]lastObject];
    _quteView.delegate = self;
    _quteView.frame = self.view.bounds;
    [self.view addSubview:_quteView];
}

#pragma mark  delegate

-(void)quteView:(ZFQuteView *)quteView {
    
    [_quteView removeFromSuperview];
    [self backMethod];
    [DSFunction removeCache];
    
    [DSFunction removeData];
}

-(void)cancleView:(ZFQuteView *)cancleView {
    
    [_quteView removeFromSuperview];
    
    
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
        }
        picker.delegate =self;
        
         [self presentViewController:picker animated:YES completion:nil];

    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    
    [_imageBtn setBackgroundImage:image forState:UIControlStateNormal];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    [self getData:image];
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark  接口调适
- (void)getData :(UIImage*)image {
    //上传方式－，不安全
//    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
//    [manager POST:<#(NSString *)#> parameters:<#(NSDictionary *)#> constructingBodyWithBlock:<#^(id<AFMultipartFormData> formData)block#> success:<#^(AFHTTPRequestOperation *operation, id responseObject)success#> failure:<#^(AFHTTPRequestOperation *operation, NSError *error)failure#>]
    
    //用于存放参数
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:[ZFUserInfo shareInstance].user_id forKey:@"user_id"];
    
    NSData *data  = UIImageJPEGRepresentation(image, 0.5);
    
    [param setObject:[data base64Encoding] forKey:@"avatar"];
    
    [param setObject:[DSFunction getTimeScamp] forKey:@"time"];
    
    
    NSArray *array = @[ APP_ID,param[@"user_id"],param[@"time"],APP_IDENGER];
    [param setObject:[DSFunction md5StringFromArray:array] forKey:@"sign"];
    
    [param setObject:APP_ID forKey:@"app_id"];
    
    [self showLoadingView];
    [DSHTTPClient DSGET:@"index.php?anu=api/1/user/update_avatar" withParam:param withSuccessBlock:^(id data) {
        
        [self hidenLoadingView];
    
  
    } withFailedBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    } withErrorBlock:^(id error) {
        
        NSLog(@"请求错误");
    }];
    
}

@end
